package com.huxiu.pagers.message;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.huxiu.R;
import com.huxiu.base.ZiXunDetailPager;
import com.huxiu.holder.MessageHolder;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.NetworkHelper;
import com.huxiu.net.model.MessageItem;
import com.huxiu.net.responses.MessageResPonse;
import com.huxiu.utils.Utils;
import com.huxiu.view.RefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class SystemMessage extends ZiXunDetailPager {


    @ViewInject(R.id.lv_tabdetail_list)
    private RefreshListView lv_tabdetail_list;// 热门新闻列表


    public SystemMessage(Activity context){
        super(context);
    }


    private int currentPage = 0;

    @Override
    protected View initView() {
        View viewAll = View.inflate(mContext, R.layout.list_detail, null);
        ViewUtils.inject(this, viewAll);
        // 给下拉刷新Listview设置监听器
//        lv_tabdetail_list.setOnRefreshingListener(new MyOnRefreshingListener());
        return viewAll;
    }



    class MyOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            System.out.println("onItemClick:" + position);
            int realPosition = position - 2;

        }

    }

    @Override
    public void initData() {

        getDataFromServer();
    }

    private boolean isRefresh = false;// 当前是否是下拉刷新

    private boolean isLoadMore = false;// 当前是否是加载更多


    class MyOnRefreshingListener implements RefreshListView.OnRefreshingListener {
        @Override
        public void onRefreshing() {
            isRefresh = true;
            // 下拉刷新业务
            getDataFromServer();
        }

        @Override
        public void onLoadingMore() {
            if (currentPage <= totalPages) {
                isLoadMore = true;
                // 加载更多业务
                getMoreDataFromServer();
            } else {
                lv_tabdetail_list.loadMoreFinished();
//                Utils.showToast(R.string.no_more);
                Toast.makeText(mContext, R.string.no_more, Toast.LENGTH_SHORT).show();
            }
        }
    }

    //上拉加载
    private void getMoreDataFromServer() {
        loadList(currentPage + 1);
    }

    //下拉刷新
    private void getDataFromServer(){
        loadList(0);
    }

    private List<MessageItem> lists = new ArrayList<>();
    private int totalPages = 0;

    //获取推荐页列表页数据
    private void loadList(final int page) {
        String url = NetworkConstants.MY_SYSTEM_MESSAGE_LIST;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("page", Integer.toString(page));
        GsonRequest<MessageResPonse> request = new GsonRequest<>(url, Request.Method.POST,
                MessageResPonse.class, true, params,
                new Response.Listener<MessageResPonse>() {
                    @Override
                    public void onResponse(MessageResPonse response) {
                        Log.e("result","response"+response.toString());
                        if (response.isSuccess()) {
                            if (page == 0){
                                lists.clear();
                            }
                            Collections.addAll(lists, response.data.datalist);
//                            currentPage = response.data.cur_page;
//                            totalPages = response.data.total_page;
                            parseListJson(lists);
                            // 如果当前是下拉刷新访问的网络，恢复下拉刷新界面
//                            if (isRefresh) {
//                                lv_tabdetail_list.refreshFinished(true);
//                                isRefresh = false;
//                            } else if (isLoadMore) {
//                                lv_tabdetail_list.loadMoreFinished();
//                                isLoadMore = false;
//                            }
                        } else {
                            Utils.showToast(response.error.message);
                            // 如果当前是下拉刷新访问的网络，恢复下拉刷新界面
//                            if (isRefresh) {
//                                lv_tabdetail_list.refreshFinished(false);
//                                isRefresh = false;
//                            } else if (isLoadMore) {
//                                lv_tabdetail_list.loadMoreFinished();
//                                isLoadMore = false;
//                            }
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Utils.showToast(R.string.generic_failure);
                // 如果当前是下拉刷新访问的网络，恢复下拉刷新界面
                if (isRefresh) {
                    lv_tabdetail_list.refreshFinished(false);
                    isRefresh = false;
                } else if (isLoadMore) {
                    lv_tabdetail_list.loadMoreFinished();
                    isLoadMore = false;
                }
            }
        });
        NetworkHelper.getInstance().addToRequestQueue(request);
    }

    private void parseListJson(List<MessageItem> lists) {
        if (!isLoadMore) {
            adapter = new SpareAdapter();
            lv_tabdetail_list.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }


    SpareAdapter adapter;
    final int VIEW_TYPE=3;

    private class SpareAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }


        @Override
        public int getViewTypeCount(){
            return VIEW_TYPE;
        }


        @Override
        public int getItemViewType(int position){
            return 1;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MessageHolder holder;
            View view = convertView;
            if (view == null) {
                view = View.inflate(mContext, R.layout.item_sysmessage_one, null);
                holder = new MessageHolder(view, mContext);
                view.setTag(holder);
            }
            holder = (MessageHolder) view.getTag();
            MessageItem item = lists.get(position);
            holder.setAdapter(adapter);
            holder.bindItem(item);//这里用来把获取到的数据传递给holder
            return view;
        }
    }
}
 